package com.ruoyi.demo.util;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.demo.domain.DeviceLog;
import com.ruoyi.demo.service.IDeviceLogService;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.service.IRoomService;

/**
 * @author 33692
 */
public class DeviceLogInsertUtil {
    public static void logDeviceChanges(Device oldDevice, Device newDevice, IDeviceLogService deviceLogService, IRoomService roomService) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        DeviceLog deviceLog = new DeviceLog();

        // 设置基本信息
        deviceLog.setUserId(loginUser.getUserId());
        deviceLog.setUserName(loginUser.getUsername());
        deviceLog.setDeviceId(Long.valueOf(newDevice.getDeviceId()));
        deviceLog.setDeviceName(newDevice.getDeviceName());
        deviceLog.setDeviceNum(newDevice.getDeviceNum());
        deviceLog.setDeviceType(newDevice.getDeviceType());
        deviceLog.setlSize(oldDevice.getSize());
        deviceLog.setlTemp(oldDevice.getTemp());
        deviceLog.setlActive(oldDevice.getIsActive());
        deviceLog.setfSize(newDevice.getSize());
        deviceLog.setfTemp(newDevice.getTemp());
        deviceLog.setfActive(newDevice.getIsActive());
        deviceLog.setRoomAddress(roomService.selectRoomByRoomId(newDevice.getRoomId()).getRoomAddress());

        StringBuilder content = new StringBuilder();

        // 记录电源状态变化（所有设备都有）
        if (oldDevice.getIsActive() != null && newDevice.getIsActive() != null && !oldDevice.getIsActive().equals(newDevice.getIsActive())) {
            String initialState = "0".equals(oldDevice.getIsActive()) ? "开启" : "关闭";
            String finalState = "0".equals(newDevice.getIsActive()) ? "开启" : "关闭";
            content.append("电源状态从【").append(initialState).append("】变为【").append(finalState).append("】；");
        }

        // 根据设备类型记录特定操作
        String deviceType = newDevice.getDeviceType();
        switch (deviceType) {
            case "0": // 空调
                // 记录温度变化
                if (oldDevice.getTemp() != null && newDevice.getTemp() != null && !oldDevice.getTemp().equals(newDevice.getTemp())) {
                    String tempChange = newDevice.getTemp() > oldDevice.getTemp() ? "调高" : "调低";
                    content.append("温度").append(tempChange).append("，从【")
                          .append(oldDevice.getTemp()).append("°C】变为【")
                          .append(newDevice.getTemp()).append("°C】；");
                }
                break;

            case "1": // 电灯
                // 只记录开关状态，已在上面处理
                break;

            case "2": // 投影仪
                // 只记录开关状态，已在上面处理
                break;

            case "3": // 窗帘
                // 记录闭合度变化
                if (oldDevice.getSize() != null && newDevice.getSize() != null && !oldDevice.getSize().equals(newDevice.getSize())) {
                    String sizeChange = newDevice.getSize() > oldDevice.getSize() ? "增加" : "减少";
                    content.append("窗帘闭合度").append(sizeChange).append("，从【")
                          .append(oldDevice.getSize()).append("%】变为【")
                          .append(newDevice.getSize()).append("%】；");
                }
                break;

            case "4": // 音响
                // 记录音量变化
                if (oldDevice.getSize() != null && newDevice.getSize() != null && !oldDevice.getSize().equals(newDevice.getSize())) {
                    String sizeChange = newDevice.getSize() > oldDevice.getSize() ? "增大" : "减小";
                    content.append("音量").append(sizeChange).append("，从【")
                          .append(oldDevice.getSize()).append("%】变为【")
                          .append(newDevice.getSize()).append("%】；");
                }
                break;

            default:
                content.append("未知设备类型的操作；");
                break;
        }

        // 移除最后一个分号和空格
        if (content.length() > 0) {
            content.setLength(content.length() - 1);
        }

        // 设置操作内容
        deviceLog.setOprationContent(content.toString());

        // 插入日志
        deviceLogService.insertDeviceLog(deviceLog);
    }
}
